<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var productExceptSelf = function (nums) {
      const res = [];
      res[0] = 1;
      //从左往右遍历
      //记录从左到当前位置前一位的乘积
      for (let i = 1; i < nums.length; i++) {
        res[i] = res[i - 1] * nums[i - 1];
      }

      let right = 1;
      //从右往左遍历
      //从左到当前位置前一位的乘积 乘上 右边元素的积
      for (let j = nums.length - 1; j >= 0; j--) {
        res[j] *= right;
        right *= nums[j];
      }

      return res;
    };
    console.log(productExceptSelf([1, 2, 3, 4]));
  </script>
</body>

</html>